//配置路由的地方
import Vue from "vue";
import VueRouter from 'vue-router';

//引用了就得使用插件
Vue.use(VueRouter);
// 引入store
import store from '@/store/index';
// //引入一级路由主件
// import Home from "@/pages/Home";
// import Search from "@/pages/Search";
// import Login from "@/pages/Login";
// import Register from "@/pages/Register";
// import Detail from "@/pages/Detail";
// import AddCartSuccess from "@/pages/AddCartSuccess";
// import ShopCart from "@/pages/ShopCart";
// import Trade from "@/pages/Trade";
// import Pay from "@/pages/Pay";
// import PaySuccess from "@/pages/PaySuccess";
// import Center from "@/pages/Center";
// // 引入二级路由组件
import MyOrder from "@/pages/Center/myOrder";
import GroupOrder from "@/pages/Center/groupOrder";

/*当打包构建应用时，JavaScript包会变得非常庞大，影响页面加载
如果我们能把不同；路由对应的组件分割成不同的代码块，
然后当路由被访问的时候在加载对应组件，这样就更加高效了*/

//配置路由
let router = new VueRouter({
    //配置路由
    routes: [
        // { path: '/search/:keyword', component: Search, meta: { show: true } ,name:'Search'}, // 原版 其它路由懒加载
        { path: '*', redirect: "/home" },
        { path: '/home', component: ()=>import("@/pages/Home"), meta: { show: true } },
        { path: '/login', component: ()=>import("@/pages/Login"), meta: { show: false } },
        { path: '/register', component: ()=>import("@/pages/Register"), meta: { show: false } },        
        { path: '/search/:keyword?', component: ()=>import("@/pages/Search"), meta: { show: true }, name: 'Search' },
        // 详情页面
        { path: '/detail/:skuid', component: ()=>import("@/pages/Detail"), meta: { show: true } },
        { path: '/addcartsuccess', component: ()=>import("@/pages/AddCartSuccess"), meta: { show: true }, name: 'addcartsuccess' },
        { path: '/shopcart', component: ()=>import("@/pages/ShopCart"), meta: { show: true }, name: "shopcart" },
        { 
            path: '/trade', 
            component: ()=>import("@/pages/Trade"), 
            meta: { show: true },
            // 路由独享守卫
            beforeEnter:(to,from,next)=>{
                // 去交易页面，必须是从购物车来
                 if(from.path=="/shopcart"){
                    next();
                 }else{
                    // 其它路由组件而来，停留在当前
                    next(false);
                 }
            }
        },
        { 
            path: '/pay', 
            component: ()=>import("@/pages/Pay"), 
            meta: { show: true },
            beforeEnter:(to,from,next)=>{
                if(from.path=='/trade'){
                    next();
                }else{
                    next(false);
                }
            }
        },
        { path: '/paysuccess', component: ()=>import("@/pages/PaySuccess"), meta: { show: true } },
        {
            path: '/center',
            component: ()=>import("@/pages/Center"),
            meta: { show: true },
            // 二级路由组件
            children: [
                {
                    path: "myorder",
                    component: MyOrder,
                }, {
                    path: "grouporder",
                    component: GroupOrder,
                },{
                    path:"/center",
                    redirect:'/center/myorder',
                }
            ]
        },

    ],
    scrollBehavior(to, from, savedPosition) {
        // ...
        return { x: 0, y: 0 }
    }
})
// 全局守卫：前置守卫（在路由跳转之间进行判断）
router.beforeEach(async (to, from, next) => {
    // to:可以获取到你要跳转到哪个路由信息
    // from：可以获取到你从哪个路由而来的信息
    // next: 放行函数--next()放行--next(path)放行的指定路由  --next(false)
    next();
    console.log("routertoken------" + store.state.user.token)
    let token = store.state.user.token;
    let name = store.state.user.userInfo.name
    if (token) {
        // 用户已经登录了还想去login[不能去，停留在首页]
        if (to.path == '/login' || to.path == '/register') {
            next('/home')
        } else {
            // 登录了，但是去的不是login
            if (name) {
                next();
            } else {
                try {
                    await store.dispatch('getUserInfo');
                    next();
                } catch (error) {
                    //token失效了获取不到用户信息，从新登录
                    //清楚token
                    await store.dispatch('userLogout');
                    next('/login');
                }
            }
        }
    } else {
        // 未登录
        let toPath=to.path;
        if(toPath.indexOf('/trade')!=-1||toPath.indexOf('/pay')!=-1||toPath.indexOf('/center')!=-1){
            // 把未登录的时候去向没有去成的信息,存储于地址栏中【路由】
            next('/login?redirect='+toPath);
        }else{
            next();
        }
    }
})


export default router;